﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using MpSoft.Library.BUSS;
using MpSoft.Library.CommonLibrary;
using System.Net.Configuration;
using System.Net.Mail;
using System.Net;
using MpSoft.Basic.DataAccess;
using System.Configuration;
using MpSoft.Library.BUSS.Settings;
namespace MpSoft.Library.CommonLibrary
{
    public class HtmlString
    {
        public static string BannerInfo(string src, string height, string width, 
            bool isFlash, bool isLink, string link)
        {
            StringBuilder sb = new StringBuilder();
            if (isFlash)
            {
                sb.Append("<object height=\"" + height + "\" width=\"" + width + "\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\">");
                sb.Append("<param name=\"movie\" value=\"" + src + "\"");
                sb.Append("<param name=\"quality\" value=\"high\">");
                sb.Append("<embed height=\"" + height + "\" width=\"" + width + "\" src=\"" + src + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" wmode=\"transparent\">");
                sb.Append("</object>");
            }
            else
            {
                if (isLink)
                {
                    sb.AppendFormat("<a href=\"{0}\"><img src=\"{1}\" height=\"{2}\" width=\"{3}\" /></a>", link,
                        src, height, width);
                }
                else
                {
                    sb.AppendFormat("<img src=\"{0}\" height=\"{1}\" width=\"{2}\" />", src, height, width);
                }
            }
            return sb.ToString();
        }
        public static void StyleSheet(HtmlHead header, string src)
        {            
            HtmlGenericControl genericHeader = null;
            genericHeader = new HtmlGenericControl("link");
            genericHeader.Attributes.Add("type", "text/css");
            genericHeader.Attributes.Add("href", src);
            genericHeader.Attributes.Add("rel", "stylesheet");
            header.Controls.Add(genericHeader);
        }
        public static void JavaScript(HtmlHead header, string src)
        {
            HtmlGenericControl genericHeader = null;
            genericHeader = new HtmlGenericControl("script");
            genericHeader.Attributes.Add("language", "javascript");
            genericHeader.Attributes.Add("type", "text/javascript");
            genericHeader.Attributes.Add("src", src);
            header.Controls.Add(genericHeader);
        }
        public static string S_Yahoo_Skyper(string Yahoo, string Skyper, string Title, string Image)
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(Yahoo))
                sb.Append("<a rel=\"nofollow\" href=\"ymsgr:SendIM?" + Yahoo + "\">" + "<img src=\"http://opi.yahoo.com/online?u=" + Yahoo + "&amp;m=g&amp;t=1&amp;l=us\" alt='" + Title + "' /></a>");
            if (!string.IsNullOrEmpty(Skyper))
                sb.Append("<a rel=\"nofollow\" href=\"skype:" + Skyper + "?chat\"><img src=\"" + Image + "\" title=\"" + Title + "\"></a>");
            return sb.ToString();
        }
        public static string S_ByType(string Nick, string Title, string Type, string Image)
        {
            StringBuilder sb = new StringBuilder();
            if (Type.Equals("2"))
            {
                sb.Append("<a rel=\"nofollow\" href=\"skype:" + Nick + "?chat\"><img src=\"" + Image + "\">&nbsp;&nbsp;" + Title + "</a>");
                return sb.ToString();
            }
            else
            {
                sb.Append("<a rel=\"nofollow\" href=\"ymsgr:SendIM?" + Nick + "\">" +
                                        "<img src=\"http://opi.yahoo.com/online?u=" + Nick + "&amp;m=g&amp;t=2&amp;l=us\" alt='" + Title + "' /></a>");
            }
            return sb.ToString();
        }
        public static string S_Adv(string url, string link, int height, int width,
            string target, bool isFlash, string title, string href)
        {
            StringBuilder sb = new StringBuilder();
            if (!isFlash)
            {
                sb.Append("<a style=\"padding:0px;\" href=\"" + href + "\" title=\"" + title + "\" target=\"" + target + "\">");
                sb.Append("<img src=\"" + url + "\" height=\"" + height + "\" width=\"" + width + "\" alt=\"" + title + "\" />");
                sb.Append("</a>");
            }
            else
            {
                sb.Append("<a style=\"padding:0px;\" href=\"" + href + "\" title=\"" + title + "\">");
                sb.Append("<object height=\"" + height + "\" width=\"" + width + "\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\">");
                sb.Append("<param name=\"movie\" value=\"" + url + "\"");
                sb.Append("<param name=\"quality\" value=\"high\">");
                sb.Append("<embed height=\"" + height + "\" width=\"" + width + "\" src=\"" + url + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" wmode=\"transparent\">");
                sb.Append("</object>");
                sb.Append("</a>");
            }
            return sb.ToString();
        }
        public static DataTable PageLink()
        {
            SqlParameter parameter = new SqlParameter();
            parameter = DbHelp.CreateParameter("@LanguageCode", LanguageInfo.GetLanguage());
            return DbHelp.ExecuteDataset("DK_PageLink_List", "DK_PageLink_List",
                parameter).Tables["DK_PageLink_List"];
        }
        public static string GetControl_PageLink(string CategoryCode)
        {
            SqlCommand command = new SqlCommand();
            command.Parameters.Add("@CategoryCode", CategoryCode);
            command.Parameters.Add("@LanguageCode", LanguageInfo.GetLanguage());
            command.CommandText = "DK_GetControl_PageLink";
            CheckValue check = new CheckValue();
            string result = "";
            using (SqlDataReader reader = DbHelp.DataReader(command))
            {
                if (reader.HasRows)
                {
                    reader.Read();
                    result = CheckValue.GetString(reader, "Control");
                }
            }
            return result;
        }
        public static string GetPageLink(string CategoryCode)
        {
            SqlCommand command = new SqlCommand();
            command.Parameters.Add("@CategoryCode", CategoryCode);
            command.CommandText = "DK_GetPageLink";
            CheckValue check = new CheckValue();
            string result = "";
            using (SqlDataReader reader = DbHelp.DataReader(command))
            {
                if (reader.HasRows)
                {
                    reader.Read();
                    result = CheckValue.GetString(reader, "Link");
                }
            }
            return result;
        }
        public static bool SendMail(string name, string subject, string content,
            string toMail)
        {
            bool rs = false;
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();
            try
            {
                MailAddress fromAddress = new MailAddress(GeneralSettings.EmailEmail, name);
                smtpClient.EnableSsl = true;
                smtpClient.Host = GeneralSettings.EmailHost;
                smtpClient.Port = GeneralSettings.EmailPort;
                message.From = fromAddress;
                message.To.Add(toMail);

                message.Subject = subject;
                message.Body = content;
                message.IsBodyHtml = true;

                smtpClient.Credentials = new NetworkCredential(GeneralSettings.EmailEmail, GeneralSettings.EmailPassword);
                smtpClient.Send(message);
                rs = true;
            }
            catch (Exception)
            {
                rs = false;
            }
            return rs;
        }
        public static string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToUpper();
            return builder.ToString();
        }     
    }
}
